'use strict';

const Controller = require('egg').Controller;

/**
* @controller LineProfileController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class LineProfileController extends Controller {
  async list() {
    /**
        * @summary lineprofilelist。
        * @description lineprofilelist。
        * @router get /lineprofilelist
        * @request query String OltId
        * @request query String search
        */
    const params = this.ctx.query;
    const result = await this.ctx.service.lineProfile.linelist(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result,
    };
  }
  async remoteList(){
    /**
        * @summary remoteprofilelist。
        * @description remoteprofilelist。
        * @router get /remoteprofilelist
        * @request query String OltId
        * @request query String search
        */
    const params = this.ctx.query;
    const result = await this.ctx.service.lineProfile.remotelist(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result,
    };
  }
  async allList() {
    /**
        * @summary lineprofilealllist。
        * @description lineprofilealllist。
        * @router get /lineprofilealllist
        * @request query String OltId
        */
    const params = this.ctx.query;
    const result = await this.ctx.service.lineProfile.allList(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result,
    };
  }
  async create() {
    /**
        * @summary lineprofilecreate
        * @description lineprofilecreate 创建lineprofile，如果有remote也一并创建
        * @router post /lineprofilecreate
        * @request body lineAndRemote value 传入参数
        */
    const params = this.ctx.request.body;
    let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
    const result = await this.ctx.service.lineProfile.create(params);
    global.log.desc = ` ${oltResult.name} ${oltResult.oltIP} `+result.msg
    this.ctx.body = {
      status: 200,
      data: result,
    };
  }
  async delete() {
    const params = this.ctx.query;
    let lineProfileResult = await this.ctx.model.LineProfile.findOne({_id:params.id})
    let oltResult = await this.ctx.model.Olt.findOne({_id:lineProfileResult.oltId})
    const result = await this.ctx.service.lineProfile.delete(params);
    global.log.desc = ` ${oltResult.name} ${oltResult.oltIP} `+result.msg
    this.ctx.body = {
      status: 200,
      data: result,
    };
  }
  async edit() {
    const params = this.ctx.request.body;
    let oltResult = await this.ctx.model.Olt.findOne({_id:params.oltId})
    const result = await this.ctx.service.lineProfile.edit(params);
    global.log.desc = ` ${oltResult.name} ${oltResult.oltIP} `+result.msg
    this.ctx.body = {
      status: 200,
      data: result,
    };
  }
  async checkLineProfileName() {
    const {name,OltId} = this.ctx.query;
    console.log(name);
    let result = await this.ctx.service.lineProfile.checkLineProfileName(name,OltId);
    this.ctx.body = {
      flag:result
    }
  }
  async getLineProfile(){
    /**
    * @summary 获取lineProfile详细信息
    * @description 获取lineProfile详细信息
    * @router get /getLineProfileDetail
    * @request query String OltId
    * @request query String id
    */
    const params = this.ctx.query;
    const result = await this.ctx.service.lineProfile.lineProfileResult(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result,
    };
  }
  async getRemoteProfile(){
    /**
    * @summary 获取RemoteProfile详细信息
    * @description 获取RemoteProfile详细信息
    * @router get /getRemoteProfileDetail
    * @request query String OltId
    * @request query String id
    */
    const params = this.ctx.query;
    const result = await this.ctx.service.lineProfile.remoteProfileResult(params);
    this.ctx.body = {
      msg: '',
      status: 200,
      data: result,
    };
  }
}

module.exports = LineProfileController;
